﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

public partial class Controls_BackEnd_ProductEdit : UserControl
{



    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        GetData();
    }


    /// <summary>
    /// 
    /// </summary>
    private void GetData()
    {
        BindFather();
        Products = Config.Id > 0 ? new Products { ProductID = Config.Id }.Products_GetByProductID() :
        new Products();
        CheckPermission();

    }


    /// <summary>
    /// 
    /// </summary>
    private void CheckPermission()
    {
        Session[SessionManager.SessionControl] = "~/Controls/Common/FileManager.ascx";
        var permission = (UserFunctions)Session[SessionManager.SessionPermission];
        btnSave.Visible = Config.Id > 0 ? permission.IsUpdate : permission.IsInsert;
        btnReset.Visible = permission.IsInsert;
        if (!permission.IsInsert && !permission.IsUpdate) return;
        txtImage.Attributes.Add("onclick", "openUpload();");
        if (string.IsNullOrEmpty(txtImage.Text))
            txtImage.Attributes.Add("value", "Click Heer To Upload.");
    }


    private string GetUrlImage()
    {
        try
        {
            var url = hdfimg.Value.ToLower();
            var link = Config.ImageUrl.ToLower();
            return url.Replace(link, string.Empty);

        }
        catch { }
        return string.Empty;
    }

    /// <summary>
    /// 
    /// </summary>
    protected Products Products
    {
        get
        {
            var products = (Products)Session[SessionManager.SessionData] ?? new Products();
            try
            {
                products.CateID = int.Parse(ddlcate.SelectedValue);
                products.ProductCode = txtCode.Text;
                products.ProductName = txtName.Text;
                products.Alias = txtAlias.Text;
                products.isNew = cbNew.Checked;
                products.Avarta = GetUrlImage();
                products.Region = int.Parse(ddlRegion.SelectedValue);
                products.UnitID = int.Parse(ddlUnit.SelectedValue);
                products.Content = AjaxEditor1.Content;
                products.isHot = cbHot.Checked;
                products.Currentcy = ddlCurency.SelectedValue;
                if (!string.IsNullOrEmpty(txtStartDate.Text))
                    products.Since = DateTime.Parse(txtStartDate.Text);
                if (!string.IsNullOrEmpty(txtDeadline.Text))
                    products.Deadline = DateTime.Parse(txtDeadline.Text);
                if (!string.IsNullOrEmpty(txtPrice.Text))
                    products.Price = decimal.Parse(txtPrice.Text);
                if (!string.IsNullOrEmpty(txtAmount.Text))
                    products.Amount = decimal.Parse(txtAmount.Text);
                products.ShopOwner = Session[SessionManager.SessionUserName].ToString();

            }
            catch { }
            return products;
        }
        set
        {
            Session[SessionManager.SessionData] = value;
            txtCode.Text = value.ProductCode;
            txtAlias.Text = value.Alias;
            txtName.Text = value.ProductName;
            cbNew.Checked = value.isNew;
            cbHot.Checked = value.isHot;
            AjaxEditor1.Content = value.Content;
            ddlcate.SelectedValue = value.CateID.ToString();
            if (value.UnitID > 0)
                ddlUnit.SelectedValue = value.UnitID.ToString();
            if (value.Region > 0)
                ddlRegion.SelectedValue = value.Region.ToString();
            txtStartDate.Text = value.Since.ToString("MM/dd/yyyy");
            txtDeadline.Text = value.Deadline.ToString("MM/dd/yyyy");
            txtPrice.Text = string.Format("{0:##,##,##}", value.Price);
            txtAmount.Text = string.Format("{0:##,##,##}", value.Amount);
            if (!string.IsNullOrEmpty(value.Currentcy))
                ddlCurency.SelectedValue = value.Currentcy;
            if (string.IsNullOrEmpty(value.Avarta)) return;
            hdfimg.Value = Config.ImageUrl + value.Avarta;
            txtImage.Text = Config.ImageUrl + value.Avarta;
            ImgAvarta.ImageUrl = Config.ImageUrl + value.Avarta;
        }
    }




    /// <summary>
    /// 
    /// </summary>
    private void BindFather()
    {
        ddlcate.Items.Clear();
        var list = new ProductCategories().ProductCategories_GetByCondition(" status=1");
        foreach (var categoriese in list)
            ddlcate.Items.Add(new ListItem { Value = categoriese.CateId.ToString(), Text = categoriese.CateName });
        var listregion = new Region().Region_GetByCondition(string.Empty);
        foreach (var region in listregion)
            ddlRegion.Items.Add(new ListItem { Value = region.ReID.ToString(), Text = region.ReName });
        var listunit = new Units().Units_GetByCondition(string.Empty);
        foreach (var unitse in listunit)
            ddlUnit.Items.Add(new ListItem { Value = unitse.uId.ToString(), Text = unitse.uName });

    }




    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AllCommand(object sender, EventArgs e)
    {
        switch (((Control)sender).ID)
        {
            case "btnReset":
                Products = new Products();
                Config.Id = 0;
                break;
            case "btnBack":
                Config.Back();
                break;
            case "btnSave":
                var error = Products.Save();
                if (error > 0)
                {
                    txtImage.Text = Config.ImageUrl + Products.Avarta;
                    ImgAvarta.ImageUrl = Config.ImageUrl + Products.Avarta;
                    Products.ProductID = Config.Id = error;
                    Config.Message(Page, Resources.Resource.SaveSuccess, Resources.Resource.Messenger);
                    return;
                }
                switch (error)
                {
                    case -1:
                        Config.Message(Page, string.Format(Resources.Resource.ObjectExisted, Resources.Resource.Product), Resources.Resource.Warning);
                        break;
                    default:
                        Config.Message(Page, Resources.Resource.ErrorMsg, Resources.Resource.Warning);
                        break;
                }
                break;

        }

    }


}

